/* eslint valid-jsdoc: "off" */

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = (appInfo) => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = (exports = {})

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1715823498639_1072'

  config.security = {
    csrf: {
      enable: false
    }
  }

  config.jwt = {
    secret: 'sonve', // 自定义token的加密条件字符串
    expires: '7d'
  }

  // add your middleware config here
  config.middleware = ['result', 'authorization']

  config.authorization = {
    enable: true,
    match(ctx) {
      // 1. 过滤是否404
      const apiRoutes = ctx.router.stack?.map((item) => item.path)
      if (!apiRoutes.includes(ctx.url)) {
        return false
      }

      // 2. 白名单免token校验
      const white = ['/api/user/login', '/api/user/register', '/api/user/getCaptcha', '/api/user/refreshToken', '/api/user/hasAdmin']
      if (white.includes(ctx.url)) {
        return false
      }

      // 3. 黑名单强制token校验
      const black = ['/api/test']
      if (black.includes(ctx.url)) {
        return true
      }

      // 4. 通用正则校验
      const reg = /^\/api\/(user|role)(\/.*)?/
      return reg.test(ctx.url)
    }
  }

  // sequelize 配置
  config.sequelize = {
    dialect: 'mysql',
    // host: '172.16.154.21', // 公司
    // host: '192.168.6.117', // 家
    // host: '192.168.253.128', // 服务器
    host: '127.0.0.1', // 本地
    port: 3306,
    username: 'root',
    password: 'root',
    database: 'egg-service',
    timezone: '+08:00'
  }

  // add your user config here
  const userConfig = {
    // myAppName: 'egg',
  }

  return {
    ...config,
    ...userConfig
  }
}
